package com.yidu.hbats.service.TrainingPlan;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.action.vo.AtsPlanCurriculumvo;
import com.yidu.hbats.dao.AtsPlanCurriculumMapper;
import com.yidu.hbats.model.AtsEmpAthleteResults;
import com.yidu.hbats.model.AtsEmpAthleteScheme;
import com.yidu.hbats.model.AtsPlanCurriculum;
import com.yidu.hbats.model.AtsPlanItem;
import com.yidu.springbootdemo.permission.dao.TuserMapper;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.mysql.cj.Session;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import sun.awt.SunHints;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 朱星润
 * @类的描述——课计划表
 * @date 2019/10/30下午 02:19
 */
@Service
public class AtsPlanCurriculumServiceImpl implements AtsPlanCurriculumService {
    @Resource
    AtsPlanCurriculumMapper atsPlanCurriculumMapper;
    @Resource
    TuserMapper managerModelMapper;
    //运动员和计划的接口
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    //训练指标
    @Resource
    AtsPlanItemService atsPlanItemService;

    @Override
    public int count(HttpServletRequest request, AtsPlanCurriculum atsPlanCurriculum) {

        HttpSession session = request.getSession();
        Tuser user = (Tuser) session.getAttribute("user");

        int count = 0;
        if(user.getCompany()!=null && !"".equals(user.getCompany())){
            if (user.getCompanyNum()=="2"||"2".equals(user.getCompanyNum())) {
                atsPlanCurriculum.setCoachName(user.getCompanyName());
                atsPlanCurriculum.setCoachId(Integer.valueOf(user.getFilId()));
                count = atsPlanCurriculumMapper.countCoach(atsPlanCurriculum);
            } else if (user.getCompanyNum()=="4"||"4".equals(user.getCompanyNum())) {
                atsPlanCurriculum.setAthleteName(user.getCompanyName());
                atsPlanCurriculum.setAthleteId(Integer.valueOf(user.getFilId()));
                count = atsPlanCurriculumMapper.countAthlete(atsPlanCurriculum);
            } else {
                count = atsPlanCurriculumMapper.countAll(atsPlanCurriculum);
            }
        }else {
            count = atsPlanCurriculumMapper.countAll(atsPlanCurriculum);
        }

        return count;
    }

    @Override
    public List<AtsPlanCurriculum> selectAll(HttpServletRequest request, AtsPlanCurriculum atsPlanCurriculum, Integer pageNum, Integer pageSize) {

        HttpSession session = request.getSession();
        Tuser user = (Tuser) session.getAttribute("user");

        PageHelper.startPage(pageNum, pageSize);
        List<AtsPlanCurriculum> list = null;
        if(user.getCompany()!=null && !"".equals(user.getCompany())){
            if (user.getCompanyNum()=="2"||"2".equals(user.getCompanyNum())) {
                atsPlanCurriculum.setCoachName(user.getCompanyName());
                atsPlanCurriculum.setCoachId(Integer.valueOf(user.getFilId()));
                list = atsPlanCurriculumMapper.findCoach(atsPlanCurriculum);
            } else if (user.getCompanyNum()=="4"||"4".equals(user.getCompanyNum())) {
                atsPlanCurriculum.setAthleteName(user.getCompanyName());
                atsPlanCurriculum.setAthleteId(Integer.valueOf(user.getFilId()));
                list = atsPlanCurriculumMapper.findAthlete(atsPlanCurriculum);
            } else {
                list = atsPlanCurriculumMapper.selectAll(atsPlanCurriculum);
            }
        }else {
            list = atsPlanCurriculumMapper.selectAll(atsPlanCurriculum);
        }


        return list;
    }

    @Override
    public int updateIsva(AtsPlanCurriculum atsPlanCurriculum) {

        atsPlanCurriculum.setIsva(0);
        int rows = atsPlanCurriculumMapper.updateIsva(atsPlanCurriculum);

        return rows;
    }

    @Override
    public int delAll(String id) {
        String[] ids = id.split(",");
        int rows=0;

        for(int i = 0;i<ids.length;i++){
            //删除计划同时删除下面指标
            AtsPlanItem atsPlanItem=new AtsPlanItem();
            atsPlanItem.setIsva(0);
            atsPlanItem.setPid(Integer.valueOf(ids[i]));
            //删除对应的运动员
            AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
            atsEmpAthleteScheme.setIsva(0);
            atsEmpAthleteScheme.setPid(Integer.valueOf(ids[i]));

            AtsPlanCurriculum atsPlanCurriculum =new AtsPlanCurriculum();
            atsPlanCurriculum.setCurriculumId(Integer.valueOf(ids[i]));
            atsPlanCurriculum.setIsva(0);
            rows=updateIsva(atsPlanCurriculum);
            atsEmpAthleteSchemeService.updatePidFive(atsEmpAthleteScheme);
            atsPlanItemService.updateOrdeletefive(atsPlanItem);
            rows++;
        }
        return rows;
    }


    @Override
    public void insertSelective(AtsPlanCurriculumvo record) {
        AtsPlanCurriculum atsPlanCurriculum = new AtsPlanCurriculum();
        atsPlanCurriculum.setSportId(record.getSportId());
        Date utilDate = new Date();//util.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        record.setCreatTime(sqlDate);
        BeanUtils.copyProperties(record, atsPlanCurriculum);
        atsPlanCurriculumMapper.insertSelective(atsPlanCurriculum);
        AtsPlanItem atsPlanItem = new AtsPlanItem();
        atsPlanItem.setRemark("1");
        atsPlanItem.setIsva(1);
        atsPlanItem.setCreatTime(sqlDate);
        atsPlanItem.setCreater(record.getCreater());
        //阶段传5 靠这个区别和查询
        atsPlanItem.setPlandate("5");
        atsPlanItem.setPid(atsPlanCurriculum.getCurriculumId());
        atsPlanItem.setPname(atsPlanCurriculum.getCurriculumName());
        boolean f = record.getItemNameone().contains(",");
        if (f == true) {
            String[] arr = record.getItemNameone().split(",");
            String[] arrtwo = record.getItemValueone().split(",");
            for (int i = 0; i < arr.length; i++) {
                String s = arr[i];
                atsPlanItem.setItemName(s);
                String sa = arrtwo[i];
                atsPlanItem.setItemValue(sa);
                atsPlanItemService.insertSelective(atsPlanItem);
            }

        } else {
            atsPlanItem.setItemName(record.getItemNameone());
            atsPlanItem.setItemValue(record.getItemValueone());
            atsPlanItemService.insertSelective(atsPlanItem);
        }
        //new出计划教练员关联表
        AtsEmpAthleteScheme atsEmpAthlete = new AtsEmpAthleteScheme();
        atsEmpAthlete.setCreater(record.getCreater());
        //传入1代表课训练计划
        atsEmpAthlete.setRemark("1");
        atsEmpAthlete.setPname(record.getCurriculumName());
        atsEmpAthlete.setPid(atsPlanCurriculum.getCurriculumId());
        boolean flag = record.getAthleteId().contains(",");
        if (flag == true) {
            String[] athleteId = record.getAthleteId().split(",");
            String[] athleteName = record.getAthleteName().split(",");
            for (int i = 0; i < athleteId.length; i++) {
                String athleteIdone = athleteId[i];
                String athleteNameone = athleteName[i];
                atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                atsEmpAthlete.setAthleteName(athleteNameone);
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        } else {
            atsEmpAthlete.setAthleteId(Integer.valueOf(record.getAthleteId()));
            atsEmpAthlete.setAthleteName(record.getAthleteName());
            atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
        }
    }

    @Override
    public void updateIsva(AtsPlanCurriculumvo record) {
        AtsPlanCurriculum atsPlanCurriculum = new AtsPlanCurriculum();
        Date utilDate = new Date();//util.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        record.setCreatTime(sqlDate);
        BeanUtils.copyProperties(record, atsPlanCurriculum);
        atsPlanCurriculumMapper.updateIsva(atsPlanCurriculum);
        AtsPlanItem atsPlanItemt = new AtsPlanItem();
        atsPlanItemt.setIsva(0);
        atsPlanItemt.setPid(record.getCurriculumId());
        int rows = atsPlanItemService.updateOrdeletefive(atsPlanItemt);
        AtsEmpAthleteScheme atsEmpAthleteScheme = new AtsEmpAthleteScheme();
        atsEmpAthleteScheme.setIsva(0);
        atsEmpAthleteScheme.setPid(record.getCurriculumId());
        int rowsone = atsEmpAthleteSchemeService.updatePidFive(atsEmpAthleteScheme);
        if (rowsone > 0) {
            //new出计划教练员关联表
            AtsEmpAthleteScheme atsEmpAthlete = new AtsEmpAthleteScheme();
            atsEmpAthlete.setCreater(record.getCreater());
            //传入1代表课训练计划
            atsEmpAthlete.setRemark("1");
            atsEmpAthlete.setPname(record.getCurriculumName());
            atsEmpAthlete.setPid(atsPlanCurriculum.getCurriculumId());
            boolean flag = record.getAthleteId().contains(",");
            if (flag == true) {
                String[] athleteId = record.getAthleteId().split(",");
                String[] athleteName = record.getAthleteName().split(",");
                for (int i = 0; i < athleteId.length; i++) {
                    String athleteIdone = athleteId[i];
                    String athleteNameone = athleteName[i];
                    atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                    atsEmpAthlete.setAthleteName(athleteNameone);
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }
            } else {
                atsEmpAthlete.setAthleteId(Integer.valueOf(record.getAthleteId()));
                atsEmpAthlete.setAthleteName(record.getAthleteName());
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        }
        if (rows > 0) {
            AtsPlanItem atsPlanItem = new AtsPlanItem();
            atsPlanItem.setRemark("1");
            atsPlanItem.setIsva(1);
            atsPlanItem.setCreatTime(sqlDate);
            atsPlanItem.setCreater(record.getCreater());
            //课传5 靠这个区别和查询
            atsPlanItem.setPlandate("5");
            atsPlanItem.setPid(record.getCurriculumId());
            atsPlanItem.setPname(record.getCurriculumName());
            boolean f = record.getItemNameone().contains(",");
            if (f == true) {
                String[] arr = record.getItemNameone().split(",");
                String[] arrtwo = record.getItemValueone().split(",");
                for (int i = 0; i < arr.length; i++) {
                    String s = arr[i];
                    atsPlanItem.setItemName(s);
                    String sa = arrtwo[i];
                    atsPlanItem.setItemValue(sa);
                    atsPlanItemService.insertSelective(atsPlanItem);
                }

            } else {
                atsPlanItem.setItemName(record.getItemNameone());
                atsPlanItem.setItemValue(record.getItemValueone());
                atsPlanItemService.insertSelective(atsPlanItem);
            }
        } else {
            AtsPlanItem atsPlanItem = new AtsPlanItem();
            atsPlanItem.setRemark("1");
            atsPlanItem.setIsva(1);
            atsPlanItem.setCreatTime(sqlDate);
            atsPlanItem.setCreater(record.getCreater());
            //课传5 靠这个区别和查询
            atsPlanItem.setPlandate("5");
            atsPlanItem.setPid(record.getCurriculumId());
            atsPlanItem.setPname(record.getCurriculumName());
            boolean f = record.getItemNameone().contains(",");
            if (f == true) {
                String[] arr = record.getItemNameone().split(",");
                String[] arrtwo = record.getItemValueone().split(",");
                for (int i = 0; i < arr.length; i++) {
                    String s = arr[i];
                    atsPlanItem.setItemName(s);
                    String sa = arrtwo[i];
                    atsPlanItem.setItemValue(sa);
                    atsPlanItemService.insertSelective(atsPlanItem);
                }

            } else {
                atsPlanItem.setItemName(record.getItemNameone());
                atsPlanItem.setItemValue(record.getItemValueone());
                atsPlanItemService.insertSelective(atsPlanItem);
            }
        }
    }

    @Override
    public AtsPlanCurriculum findByIdTwo(Integer curriculumId) {
        return atsPlanCurriculumMapper.findByIdTwo(curriculumId);
    }

    @Override
    public AtsPlanCurriculum findMaxPid(int coachId) {
        return atsPlanCurriculumMapper.findMaxPid(coachId);
    }


    @Override
    public AtsPlanCurriculum findById(Integer curriculumId) {
        AtsPlanCurriculum atsPlanCurriculum = atsPlanCurriculumMapper.findById(curriculumId);
        return atsPlanCurriculum;
    }
}
